#SQLInterface_UI.py
# -*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui, uic
import sys

import SQLInterface_Interface as interface


class mainWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        uic.loadUi("ui/SQLInterfaceUI_mainWindow.ui", self)
        
        self.connect(self.addAction,QtCore.SIGNAL('triggered()'),
                     self.onAdd)
        
        self.connect(self.addBtn,QtCore.SIGNAL('clicked()'),
                     self.onAdd)
        
        self.connect(self.openAction,QtCore.SIGNAL('triggered()'),
                     self.onOpen)
        
        self.connect(self.findBtn,QtCore.SIGNAL('clicked()'),
                     self.onFind)
        
        self.connect(self.commitBtn,QtCore.SIGNAL('clicked()'),
                     self.onClose)
        
        
        self.connection = None
        self.cursor = None


    def onAdd(self):
        self.child = addDialog()
        self.child.show()


    def onOpen(self):
        filename = QtGui.QFileDialog.getOpenFileName(self,
                                                      u'Открыть файл...', '/', "*.sqlite")
        filename = str(filename)
        self.connection, self.cursor = interface.opendb(filename)
        self.statusBar.showMessage(u"Открыто")
        
        
    def onFind(self):
        text = self.findEdit.text()
        text = str(text)
        result = interface.find(self.connection, self.cursor, text)
        self.statusBar.showMessage(u"Готово")
        print result
        for i in range(len(result)):
           self.itemsLst.insertItem(result[i][0], QtCore.QString(result[i][1]))
            
        
    def onClose(self):
        if self.connection != None:
            interface.close(self.connection, self.cursor)
            self.statusBar.showMessage(u"Закрыто")
        else:
            mes = QtGui.QMessageBox(parent = None)
            mes.setWindowTitle(u"Ошибка!")
            mes.setText(u"Файл базы данных не открыт!")
            mes.exec_()

class addDialog(QtGui.QWidget):
    def __init__(self, parent = None):
            QtGui.QWidget.__init__(self, parent)
            uic.loadUi("ui/SQLInterfaceUI_addDialog.ui", self)
            self.connect(self.okBtn,QtCore.SIGNAL('clicked()'),self.onOk)
        
    def onOk(self):
            try:
                name = str(self.nameEdit.text())
                bar = str(self.barEdit.text())
                sn = str(self.snEdit.text())
                price = float(self.priceEdit.text())
                count = int(self.countEdit.text())
                if mw.connection != None:
                    interface.add(mw.connection, mw.cursor, name, bar, sn, price, count)
                    self.close()
                else:
                    mes = QtGui.QMessageBox(parent = None)
                    mes.setWindowTitle(u"Ошибка!")
                    mes.setText(u"Файл базы данных не открыт!")
                    mes.exec_()
                    self.close()
            except:
                mes = QtGui.QMessageBox(parent = None)
                mes.setWindowTitle(u"Ошибка")
                mes.setText(u"Ошибка ввода!")
                mes.exec_()
                self.close()
        

app = QtGui.QApplication(sys.argv)

mw = mainWindow()


mw.show()
sys.exit(app.exec_())